// pages/index/store_admin/goods_edit.js
Page({

  data: {   shebeilist:{},store:[],arr:[],index:0,arr1:['打印机','复印机'],index1:0,arr2:['企业','个人'],index2:1,url:'',
  speclist:[],arr3:[],specxq:[],arrzong_id:[],arr7:[],title:'',deviceld:'',devicekey:'',devicepost:'',pintermodel:'',bonus:'',number:'',
  url1:'',arrzong:[],id:'',type:'',ar:[],ar1:[],ar2:[],flag:''

  },

  onLoad: function (options) {
   this.setData({
     id:options.id,
     type:options.type
   })
   if(options.type==2){
    this.getshebeilist()
   }
   else{this.getshebeilist1()}
   
   
  },
  onShow(){
    if(this.data.type==2){
      this.getshebeilist()
     }
     else{this.getshebeilist1()}

  },
   //获取修改设备的数据
   getshebeilist(){
     wx.request({
       url: 'https://zujieba.com/Api/storeadmingoods/edit_goods.html',
       data:{id:this.data.id,status:2,uid:wx.getStorageSync('uid')},
       success:(res)=>{
         console.log(this.data.id);
         
         console.log(res);
         let pid=res.data.data.data.pid

         let specxq=res.data.data.specxq
         let ar=[]
         let ar1=[]
         let ar2=[]
         specxq.forEach((item)=>{
           ar.push(item.title)
           ar1.push(item.spec_id)
           ar2.push(item.price)
               
         })
         this.setData({
           ar,
           ar1,
           arr7:ar2
   
         })
         let store=res.data.data.store
         store.forEach((item,index)=>{
              if(item.id==pid){
                this.setData({index:index})
              }
         })
         let speclist=res.data.data.spec
         let arr3=[]
         speclist.forEach((item)=>{
             if(item.level==1) {
                arr3.push(item)
                item.list=[]
             }
         })
         arr3.forEach((item1)=>{
            speclist.forEach((item2)=>{
               if(item1.id==item2.pid){
                  item1.list.push(item2)
                  item2.checked=false
               }
            })
         })
        
         
         
         let arr=[]
         store.forEach((item)=>{
             arr.push(item.title)
         })
         console.log(arr);
         let spec=res.data.data.data.spec
         arr3.forEach((item)=>{
           item.list.forEach((item1)=>{
             spec.forEach((item3)=>{
               item3.data.forEach((item4)=>{
                 if(item4.title==item1.title){
                   item1.checked=true
                 }
               })
             })
           })
         })
         console.log(arr3,specxq);
         this.setData({arr3,specxq,index1:res.data.data.data.statu-1,index2:res.data.data.data.statu-1,title:res.data.data.data.title,
        bonus:res.data.data.data.bonus,number:res.data.data.data.number,devicekey:res.data.data.data.devicekey,deviceld:res.data.data.data.deviceld,
      devicepost:res.data.data.data.devicepost,pic:res.data.data.data.pic,pintermodel:res.data.data.data.pintermodel})
         
         
         let shebeilist=res.data.data.data
         let pic="https://zujieba.com/file_index/"+res.data.data.data.pic
         console.log(arr);
         
         this.setData({
           shebeilist,
           store,
           arr,
           url:pic,
           speclist,
           url1:res.data.data.data.pic
         })
         console.log(this.data.arr);
         
       }
       
     })
   },
   bindPickerChange(e){
     
     
     this.setData({
       index:e.detail.value
     })
   },
   bindPickerChange1(e){
     
     
    this.setData({
      index1:e.detail.value
    })
  },
  bindPickerChange2(e){
     
     
    this.setData({
      index2:e.detail.value
    })
  },
  remove(){
     this.setData({url:''})
  },
  getimglist(){
    console.log(111);
    wx.chooseImage({
      count:1,
      complete: (res) => {
        console.log(res);
        wx.uploadFile({
          filePath: res.tempFiles[0].path,
          name: 'file',
          url: 'https://zujieba.com/index/upload/upload.html',
          success:(res1)=>{
            console.log(res1);
            let data=JSON.parse(res1.data)
            let url="https://zujieba.com/file_index/"+data.name
            this.setData({url,url1:data.name})
          }
        })
      },
    })
    
  },
  toggle3(e){
    this.setData({arr7:[],flag:1,arrzong:[]})
      console.log(e.currentTarget.dataset.index,e.currentTarget.dataset.index1);
      let index=e.currentTarget.dataset.index
      let index1=e.currentTarget.dataset.index1
      let arr3=this.data.arr3
      arr3[index1].list[index].checked=!arr3[index1].list[index].checked
      this.setData({arr3})


      let arr4=[]
      let arr5=[]
      let arr6=[]
      let arr8=[]
      let arr4_id=[]
      let arr5_id=[]
      let arr6_id=[]
      let arr8_id=[]
      arr3.forEach((item,index)=>{
        item.list.forEach((item1)=>{
            if(item1.checked==true&&index==0){
              arr4.push(item1.title)
              arr4_id.push(item1.id)
              console.log(arr4);
              
            }else if(item1.checked==true&&index==1){
               arr5.push(item1.title)
               arr5_id.push(item1.id)
               console.log(arr5);
               
            }else if(item1.checked==true&&index==2){
              arr6.push(item1.title)
              arr6_id.push(item1.id)
              console.log(arr6);
              
            }else if(item1.checked==true&&index==3){
              arr8.push(item1.title)
              arr8_id.push(item1.id)
              console.log(arr6);
              
            }
        })
      })
      console.log(arr4);
   //这是拼接title的   
      let arrzong=[]
      let arrzong_id=[]
      let str
      let str_id
      if(arr4.length!=0){
      arr4.forEach((item)=>{
        console.log(11);
        
          arr5.forEach((item1)=>{
           /* if(arr6.length==0){
            console.log(22);
            str=item+'_'+item1
            
            arrzong.push(str)}*/
          
            
            arr6.forEach((item2)=>{

              console.log(33);
              
               arr8.forEach((item3)=>{
                str=item+'_'+item1+'_'+item2+'_'+item3
                arrzong.push(str)
                console.log(arrzong);
                this.setData({arrzong})
               })
              
            })
          })
      })}
      /*else{
        arr5.forEach((item1)=>{
         
          
          arr6.forEach((item2)=>{

            console.log(33);
            
             str=item1+'_'+item2
            arrzong.push(str)
         
            
          })
        })

      }*/
      console.log(arrzong);
      
      //这是拼接id的
      if(arr4_id.length!=0){
        arr4_id.forEach((item)=>{
          console.log(11);
          
            arr5_id.forEach((item1)=>{
              /*if(arr6_id.length==0){
              console.log(22);
              str_id=item+'_'+item1
              
              arrzong_id.push(str_id)}
              else{
              */
              arr6_id.forEach((item2)=>{
  
                console.log(33);
                
                arr8_id.forEach((item3)=>{
                  str_id=item+'_'+item1+'_'+item2+'_'+item3
                  arrzong_id.push(str_id)
                  console.log(arrzong_id);
                })
                
              })
            })
        })}/*else{
          arr5_id.forEach((item1)=>{
           
            
            arr6_id.forEach((item2)=>{
  
              console.log(33);
              
              arr8_id.forEach((item3)=>{
                str_id=item1+'_'+item2+'_'+item3
                arrzong_id.push(str_id)
                console.log(arrzong_id);
              })
              
            })
          })
  
        }
        console.log(arrzong_id);*/
        
      this.setData({
        arrzong_id
      })
      
  },
  change6(e){
    console.log(e);
    console.log(this.data.arr7);
    
    let arr7=this.data.arr7
    arr7[e.currentTarget.dataset.i]=e.detail.value
    console.log(arr7);
    this.setData({arr7})
    
  },
  change1(e){
    this.setData({
      title:e.detail.value
    })
 },
 change2(e){
  this.setData({
    deviceld:e.detail.value
  })
},
change3(e){
this.setData({
  devicekey:e.detail.value
})
},
change4(e){
this.setData({
  devicepost:e.detail.value
})
},
change5(e){
this.setData({
  pintermodel:e.detail.value
})
},
change7(e){
this.setData({
  bonus:e.detail.value
})
},
change8(e){
this.setData({
  number:e.detail.value
})
},
sureedit1(){
  let uid=wx.getStorageSync('uid')
  let pid=this.data.store[this.data.index].id
  console.log(pid,this.data.store,this.data.index);
  
  let title=this.data.title
   let deviceld=this.data.deviceld
   let devicepost=this.data.devicepost
    let devicekey=this.data.devicekey
     let pintermodel=this.data.pintermodel
      let number=this.data.number
       let bonus=this.data.bonus
        let mune=2
         let statu=parseInt(this.data.index1)+1
          let id=this.data.id
         let pic=this.data.url1
          let price=this.data.arr7
            let spec_title=this.data.arrzong.length==0?this.data.ar:this.data.arrzong
              let  spec_id=this.data.arrzong_id.length==0?this.data.ar1:this.data.arrzong_id
              let params={uid,pid,id,title,deviceld,devicekey,devicepost,pintermodel,number,bonus,mune,statu,pic,price,spec_id,spec_title,status:1,type:1,color:1,}
              console.log(params);
              
  wx.request({
    url: 'https://zujieba.com/Api/storeadmingoods/edit_goods.html',
    method:'POST',
    data:params,
    success:(res)=>{
      console.log(res);
      if(res.data.success==1){
        wx.navigateBack({
          complete: (res) => {},
        })
        wx.showToast({
          title: '修改成功',
        })
      }else{
        wx.showToast({
          title: res.data.tips,
          icon:'none'
        })
      }
      
    }
  })

},
 //获取修改设备的数据
 getshebeilist1(){
  wx.request({


    url: 'https://zujieba.com/Api/storeadmingoods/edit_sharer_goods.html',
    data:{id:this.data.id,status:2,pid:wx.getStorageSync('uid')},
    method:'POST',
    success:(res)=>{
      //拿到返回的specxq数组
      let specxq=res.data.data.specxq
      let ar=[]
      let ar1=[]
      let ar2=[]
      specxq.forEach((item)=>{
        ar.push(item.title)
        ar1.push(item.spec_id)
        ar2.push(item.price)
            
      })
      this.setData({
        ar,
        ar1,
        arr7:ar2

      })
      
      console.log(this.data.id);
      
      console.log(res);
      
      let store=res.data.data.store
      let speclist=res.data.data.spec
      let arr3=[]
      speclist.forEach((item)=>{
          if(item.level==1) {
             arr3.push(item)
             item.list=[]
          }
      })
      arr3.forEach((item1)=>{
         speclist.forEach((item2)=>{
            if(item1.id==item2.pid){
               item1.list.push(item2)
               item2.checked=false
            }
         })
      })
     
      
      
      let arr=[]
      store.forEach((item)=>{
          arr.push(item.title)
      })
      console.log(arr);
      let spec=res.data.data.data.spec
      arr3.forEach((item)=>{
        item.list.forEach((item1)=>{
          spec.forEach((item3)=>{
            item3.data.forEach((item4)=>{
              if(item4.title==item1.title){
                item1.checked=true
              }
            })
          })
        })
      })
      
      let pid=res.data.data.data.pid
      let indexstr=''
       store.forEach((item,index)=>{
           if(item.id==pid){
               indexstr=index
           }
       })
      console.log(indexstr);
      this.setData({arr3,specxq,index1:res.data.data.data.statu-1,index2:res.data.data.data.statu-1,title:res.data.data.data.title,index:indexstr,
     bonus:res.data.data.data.bonus,number:res.data.data.data.number,devicekey:res.data.data.data.devicekey,deviceld:res.data.data.data.deviceld,
   devicepost:res.data.data.data.devicepost,pic:res.data.data.data.pic,pintermodel:res.data.data.data.pintermodel})
      
      
      let shebeilist=res.data.data.data
      let pic="https://zujieba.com/file_index/"+res.data.data.data.pic
      this.setData({
        shebeilist,
        store,
        arr,
        url:pic,
        speclist,
        url1:res.data.data.data.pic
      })
      
    }
    
  })
},
//共享商提交
sureedit11(){
  let uid=wx.getStorageSync('uid')
 let pid=this.data.store[this.data.index].id
  let title=this.data.title
   let deviceld=this.data.deviceld
   let devicepost=this.data.devicepost
    let devicekey=this.data.devicekey
     let pintermodel=this.data.pintermodel
      let number=this.data.number
       let bonus=this.data.bonus
        let mune=2
         let statu=parseInt(this.data.index1)+1
          let id=parseInt(this.data.id)
         let pic=this.data.url1
          let price=this.data.arr7
            let spec_title=this.data.arrzong.length==0?this.data.ar:this.data.arrzong
              let  spec_id=this.data.arrzong_id.length==0?this.data.ar1:this.data.arrzong_id
              let params={uid,pid,id,title,deviceld,devicekey,devicepost,pintermodel,number,bonus,mune,statu,pic,price,spec_id,spec_title,status:1,type:1,color:1,}
              console.log(params);
              
  wx.request({
    url: 'https://zujieba.com/Api/storeadmingoods/edit_sharer_goods.html',
    method:'POST',
    data:params,
    success:(res)=>{
      console.log(res);
      if(res.data.success==1){
        wx.navigateBack({
          complete: (res) => {},
        })
        wx.showToast({
          title: '修改成功',
        })
      }else{
        wx.showToast({
          title: res.data.tips,
          icon:'none'
        })
      }
      
    }
  })

},
})